<template>
	<view class="page-box">
		<view class="order" v-for="(item, index) in list" :key="item.goodsId">
			<view @click="toDetail(item)" class="item">
				<view class="left">
					<image :src="item.image.split(',')[0]" mode="aspectFill"></image>
				</view>
				<view class="content">
					<view class="title u-line-4">{{ item.introduce }}</view>
				</view>
				<view class="right">
					<view style="color: #d53b33;" class="price">
						￥{{ item.price }}
					</view>
					<view v-if="item.status == '0'" class="upanddownStatus">已上架</view>
					<view v-if="item.status == '1'" class="upanddownStatus">已下架</view>
				</view>
			</view>
			<view class="bottom">
				<view @click="upanddown(item)" class="exchange btn">
					<spna v-if="item.status == '0'">下架</spna>
					<spna v-if="item.status == '1'">上架</spna>
				</view>
				<view @click="editBtn(item)" class="evaluate btn">编辑</view>
				<view @click="deleteBtn(item)" class="evaluate btn">删除</view>
			</view>
		</view>
		<u-loadmore :status="loadstatus" @loadmore="loadMore" bgColor="#f2f2f2"></u-loadmore>
	</view>
</template>

<script setup>
	import {
		reactive,
		ref
	} from 'vue';
	import {
		deleteApi,
		getMyUnusedListApi,
		upanddownApi
	} from '../../api/goods.js'
import { onReachBottom, onShow } from '@dcloudio/uni-app';
	const list = ref([])
	//列表查询的参数
	const parm = reactive({
		userId:uni.getStorageSync("userId"),
		type:"0",
		currentPage:1,
		pageSize:10
	})
	//总页数
	const pages = ref(0)
	//获取列表数据
	const getMyUnusedList = async() =>{
		let res = await getMyUnusedListApi(parm)
		if(res && res.code == 200){
			console.log(res)
			pages.value = res.data.pages
			list.value = list.value.concat(res.data.records)
		}
	}
	
	//加载更多
	const loadMore = ()=>{
		if(parm.currentPage >= pages.value){
			loadstatus.value = "nomore"
			return;
		}
		loadstatus.value = "loading"
		parm.currentPage = ++ parm.currentPage
		getMyUnusedList()
	}
	//触底加载
	onReachBottom(()=>{
		if(parm.currentPage >= pages.value){
			loadstatus.value = "nomore"
			return;
		}
		loadstatus.value = "loading"
		parm.currentPage = ++ parm.currentPage
		getMyUnusedList()
	})
	const loadstatus = ref('loadmore')
	//编辑
	const editBtn = (item)=>{
		uni.navigateTo({
			url:"../edit/edit?goods=" + JSON.stringify(item)
		})
	}
	//跳转详情页
	const toDetail = (item) => {
		uni.setStorageSync('goodsId', item.goodsId)
		uni.navigateTo({
			url: "../unused_detail/unused_detail?goods=" + JSON.stringify(item)
		})
	}
	//上下架
	const upanddown = async(item)=>{
		let res = await upanddownApi({
			goodsId:item.goodsId,
			status:item.status=='1' ? '0' : '1'
		})
		if(res && res.code == 200){
			list.value = []
			parm.currentPage = 1
			getMyUnusedList()
		}
	}
	//删除
	const deleteBtn = (item)=>{
		uni.showModal({
			title:"确定删除吗？",
			success:function(res){
				if(res.confirm){
					deleteitem(item)
				}				
			}
		})
	}
	const deleteitem = async(item)=>{
		let res = await deleteApi({
			goodsId:item.goodsId,
			status:item.deleteStatus='1'
		})
		if(res && res.code == 200){
			list.value = []
			parm.currentPage = 1
			getMyUnusedList()
		}
	}
	onShow(()=>{
		list.value = []
		parm.currentPage = 1
		getMyUnusedList()
	})
</script>

<style lang="scss">
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}
			}

			.right {
				margin-left: auto;
				padding-top: 50rpx;
				text-align: right;
				.upanddownStatus{
					padding-top: 20rpx;
					color: #d79b02;
				}
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding-right: 70px;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-right: 10px;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}
</style>